package com.tangdi.production.mpaychl.service.impl.pap;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.RandomStringUtils;

import com.alibaba.fastjson.JSON;
import com.tangdi.production.mpbase.util.HttpUtil;
import com.tangdi.production.mpbase.util.MapUtils;
import com.tangdi.production.tdbase.util.MD5Util;

public class TestMain {

	//8190500263	19050600001886	6B11CAC5B71881B47D07EAD34065BE04
	
    public static String mch_Id = "C1563790357046B93";
    public static final String SIGNKEY = "CSKC0FB93254AA48F";
/*    public static String ORG_NO = "8180900254";
    public static String custId = "18092500001879";
    public static final String SIGNKEY = "2280C7E72DCC3290DE746F0A4BEE5489";*/
/*    public static String ORG_NO = "8181200260";
    public static String custId = "18121900001886";
    public static final String SIGNKEY = "320806C70E95DEA35173CCD4AD90179D";*/

	public static String url = "http://120.78.184.88/pap/trans/cashier/recharge.htm"; // 服务器地址
	// public static String url = "http://localhost:8080/mpcctp/"; // 服务器地址
	// public static String url = "http://119.28.115.194:8081/mpcctp/"; // 服务器地址

	public static void main(String[] args) throws Exception {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		paramMap.put("mchId", mch_Id);
		paramMap.put("outTradeNo",
				DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + RandomStringUtils.randomAlphabetic(7));
		paramMap.put("body", "123");

		paramMap.put("totalFee", 1.00);// 交易金额元
		paramMap.put("callbackUrl", "http://58.220.30.99:8081/qrcode-starter/returnTest1");
		paramMap.put("service", "al");

		paramMap.put("sign", sign(paramMap, SIGNKEY));
		System.out.println("url:" + url);

		
		String paramStr = JSON.toJSONString(paramMap);
		System.out.println(paramStr);
		Map<String, Object> requestHead = new LinkedHashMap<>();
		requestHead.put("Content-Type", "application/json;Charset=UTF-8;");
		String resStr = HttpUtil.httpRequest(url, paramStr, "POST", "UTF-8", requestHead);
		System.out.println(resStr);
		System.out.println(JSON.parseObject(resStr).getString("payInfo"));
	}

	private static String sign(Map<String, Object> paramMap, String signKey) {
		String signStr = MapUtils.getSignStrByTreeMap(paramMap, true);
		signStr += "&key=" + signKey;
		// String signValue = MD5.encryption(signStr).toUpperCase();
		String signValue = MD5Util.digest(signStr).toLowerCase();
		return signValue;
	}
}
